package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import database.Conn;

public class UseService {
	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblUseservice";
	public boolean AddUseService(int id_service, int id_room,int id_customer,int charge,int status){
		connect = Conn.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(id_service,id_room," +
					"id_customer,charge,status) VALUES (?,?,?,?)");
			pstm.setInt(1,id_service);
			pstm.setInt(2,id_room);
			pstm.setInt(3,id_customer);
			pstm.setInt(4,charge);
			pstm.setInt(5, status);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
		
	}
	public boolean DeleteUseService(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateUseService(int id,int id_service, int id_room,int id_customer,int charge,int status){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET id_service=?,id_room=?," +
					"id_customer=?,charge=?,status=?  WHERE id=?");
			pstm.setInt(1,id_service);
			pstm.setInt(2,id_room);
			pstm.setInt(3,id_customer);
			pstm.setInt(4,charge);
			pstm.setInt(5,status);
			pstm.setInt(6,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.UseService> GetAllUseService(){
		connect= Conn.getConnect();
		ResultSet rs=null;
		ArrayList<entity.UseService> list= new ArrayList<entity.UseService>();
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
				entity.UseService use= new entity.UseService();
				use.setId(rs.getInt("id"));
				use.setId_room(rs.getInt("id_room"));
				use.setId_customer(rs.getInt("id_customer"));
				use.setCharge(rs.getInt("charge"));
				use.setStatus(rs.getInt("status"));
				use.setId_service(rs.getInt("id_service"));
				list.add(use);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public void GetUseServiceById(int id){
		
		connect= Conn.getConnect();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
	
			// Lay thong tin gui den controller
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	
}
